package com.wsq.coroutine.viewmodel

import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.wsq.coroutine.bean.LoginData
import com.wsq.coroutine.model.MainModel
import kotlinx.coroutines.*
import java.lang.Exception

/**
 *
 *Date:2022/5/25
 *Time:9:52
 *author:wushengqi
 */
class MainViewModel:ViewModel() {
    private var _loginModel:MutableLiveData<LoginData> = MutableLiveData<LoginData>()

    val loginModel:LiveData<LoginData> = _loginModel

    init {
        _loginModel.value = LoginData(1, "default", 10000)
    }

    fun getDemo(){
        MainScope().launch {
            try {
                for (i in 1 .. 20){
                    val result = MainModel.getDemo()
                    _loginModel.value = result
                    Log.e("Main:" + i, result.toString())
                }

                Log.e("Main", "完事儿了")

            }catch (e:Exception){
                Log.e("Main err", e.toString())
            }
        }
    }

    fun onClick(view: View){
        Toast.makeText(view.context, "我被点击啦！", Toast.LENGTH_SHORT).show()
    }
}